import api from '@/api'
import { isIos } from '@/utils/index'
const wx = require('weixin-js-sdk')
console.log(wx,'wx')
import {
  LgetItem,
} from '@/utils/storage'
function wxInit() {
  return new Promise((resolve, reject) => {
    console.log(isIos(),wx.readySucc)
    if (isIos() && wx.readySucc) {
      console.log(wx.readySucc,'wx.readySucc2222')
      resolve()
      return
    }
    api('config.getWxSign', {
      url: encodeURIComponent(window.location.href.split('#')[0]),
      voteId:LgetItem('voteId')
    }).then(res => {
      wx.config({
        debug: false,
        appId: res.appId,
        timestamp: res.timestamp,
        nonceStr: res.nonceStr,
        signature: res.signature,
        jsApiList: [
           'updateAppMessageShareData','updateTimelineShareData'
        ]
      })
      wx.ready(_ => {
        wx.readySucc = true
        console.log('成功')
        resolve()
        console.log('jssdk ready:', _)
      })
      wx.error(err => {
        console.log('失败1')
        reject()
        console.log('jssdk err:', err)
      })
    }).catch(err => {
      console.log('失败2')
      reject()
      console.log(err)
    })
  })
}

export {
  wx,
  wxInit
}